
export function validTime(t) {
	let time = t * 1;
	let str = '';
	if (time) {
		let h = parseInt(time / 60);
		let m = parseInt(time % 60);
		str = `${h?h+'小时':''}${m}分钟`
	}
	return str || '0分钟';
}

// 函数防抖，在事件触发后n秒执行，在这n秒内重新触发，则重新执行
let timer;
export function debounce(fn, delay = 500) {
	if (timer) clearTimeout(timer);
	timer = setTimeout(() => {
		fn();
	}, delay);
}
// 函数节流，每隔一段时间只执行一次
let timer1;
export function throttle(fn, delay = 500) {
	if (timer1) return;
	fn();
	timer1 = setTimeout(() => {
		timer1 = null;
	}, delay);
}
// 字符串转驼峰
export function tf(str) {
	let reg = /_(\w)/g;
	str = str.replace(reg, function($0, $1) {
		return $1.toUpperCase()
	})
	return str
}
// 提示
export function msg(title, duration = 1500, mask = false, icon = 'none') {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}
// 字典转换
export function formatDitcs(list, status) {
	if (typeof list === 'object' && list.length) {
		return list.find((item) => item.dictValue === status).dictLabel
	}
}
// 转换图片地址带上前缀
export function imageConversion(list) {
	if (typeof list === 'object' && list.length) {
		const IMG_PATH = uni.getStorageSync('imgPath')
		return list.map((src) => IMG_PATH + src)
	} else {
		return []
	}
}
// 返回上多少页，默认上一页
export function navigateBack(delta = 1) {
	uni.navigateBack({
		delta
	})
}
// 获取上多少页实例，默认上一页
export function getPages(prev = 2) {
	let pages = getCurrentPages();
	if (pages.length > 1) {
		//返回页面实例
		return pages[pages.length - prev].$vm;
	} else {
		return null
	}
}
// 清除缓存，只保留user
export function clearStorage() {
	let user = uni.getStorageSync('user') || {}
	uni.clearStorageSync()
	uni.setStorageSync('user', user)
}